﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using hospital_registration_system.Entity;
using LinkSql;

namespace hospital_registration_system.DAL
{
    public class Medical_recordDao
    {
        private string tableName = "medical_record";
        private string id= "id";// id号
        private string CREATED_BY = "CREATED_BY";// 创建人
        private string CREATED_TIME = "CREATED_TIME";// 创建时间
        private string UPDATED_BY = "UPDATED_BY";// 更新人
        private string UPDATED_TIME = "UPDATED_TIME";// 更新时间
        private string state = "state";// 状态
        private string appointment_id = "appointment_id";// 挂号id
        private string patient_id = "patient_id";// 病人id
        private string department_id = "department_id";// 科室id

        /// <summary>
        /// 通过对象添加就诊记录
        /// </summary>
        /// <param name="mr"></param>
        /// <returns></returns>
        public bool insert(Medical_record mr)
        {
            Dictionary<string, Object> map = new Dictionary<string, object>();
            map.Add(CREATED_BY,mr.CREATED_BY1);
            map.Add(CREATED_TIME,mr.CREATED_TIME1);
            map.Add(UPDATED_BY,mr.UPDATED_BY1);
            map.Add(UPDATED_TIME,mr.UPDATED_TIME1);
            map.Add(state,mr.State);
            map.Add(appointment_id,mr.Appointment_id);
            map.Add(patient_id,mr.Patient_id);
            map.Add(department_id,mr.Department_id);
            return MySQLUtils.insert(tableName,map);
        }

        /// <summary>
        /// 通过id删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool delete(int id)
        {
            return MySQLUtils.delete(tableName, id);
        }

        /// <summary>
        /// 通过对象修改其属性
        /// </summary>
        /// <param name="mr"></param>
        /// <returns></returns>
        public bool update(Medical_record mr)
        {
            Dictionary<string, Object> map = new Dictionary<string, object>();
            map.Add(id, mr.Id);
            map.Add(CREATED_BY, mr.CREATED_BY1);
            map.Add(CREATED_TIME, mr.CREATED_TIME1);
            map.Add(UPDATED_BY, mr.UPDATED_BY1);
            map.Add(UPDATED_TIME, mr.UPDATED_TIME1);
            map.Add(state, mr.State);
            map.Add(appointment_id, mr.Appointment_id);
            map.Add(patient_id, mr.Patient_id);
            map.Add(department_id, mr.Department_id);
            return MySQLUtils.update(tableName, map);
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public List<Medical_record> selectAll()
        {
            return GetMedical_RecordsByDataTable(MySQLUtils.selectAll(tableName));
        }

        /// <summary>
        /// 通过id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Medical_record> selectById(int id)
        {
            return GetMedical_RecordsByDataTable(MySQLUtils.selectById(tableName,id));
        }

        /// <summary>
        /// 通过map查询
        /// </summary>
        /// <param name="map"></param>
        /// <returns></returns>
        public List<Medical_record> selectByMap(Dictionary<string,Object> map)
        {
            return GetMedical_RecordsByDataTable(MySQLUtils.selectByMap(tableName, map));
        }

        /// <summary>
        /// 通过dataTable获取list
        /// </summary>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public List<Medical_record> GetMedical_RecordsByDataTable(DataTable dataTable)
        {
            List<Medical_record> list = new List<Medical_record>();
            foreach(DataRow row in dataTable.Rows)
            {
                list.Add(new Medical_record(
                    int.Parse(row["id"].ToString()),
                    row["created_by"].ToString(),
                    (DateTime)(row["created_time"]),
                    row["updated_by"].ToString(),
                    (DateTime)(row["updated_time"]),
                    int.Parse(row["state"].ToString()),
                    int.Parse(row["appointment_id"].ToString()),
                    int.Parse(row["patient_id"].ToString()),
                    int.Parse(row["department_id"].ToString())
                ));
            }
            return list;
        }

    }
}
